package com.hdc.sell.controller;

import com.hdc.sell.Service.OrderService;
import com.hdc.sell.dto.OrderDTO;
import com.hdc.sell.enums.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sun.rmi.runtime.Log;

import java.util.Map;

/**
 * @author 郭丽然
 * @program: sell
 * @date 2020/6/6 16:39
 * 功能：
 */
@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    //1接口：查询所有订单列表
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1")
                             Integer page,
                             @RequestParam(value = "size",defaultValue = "3")
                             Integer size,
                             Map<String,Object> map) {
        //1创建分页条件对象
        PageRequest request=PageRequest.of(page-1,size);
        //2通过orderService对象查询所有订单
        Page<OrderDTO> orderDTOPage = orderService.findList(request);
        //3获得的数据对象存入map
        map.put("orderDTOPage",orderDTOPage);
        map.put("currentPage",page);
        map.put("size",size);
        //4创建modelAndView，将模型数据和视图层url存入其中，并返回
        return new ModelAndView("order/list",map);
    }

    //2接口：取消订单
    @RequestMapping("/cancel")
    public ModelAndView cancel (@RequestParam("orderId") String orderId,
                                Map<String,Object> map) {
        try {
            //1根据订单号获取取消的订单
            OrderDTO orderDTO=orderService.findOne(orderId);
            //2调用orderService的cancel方法完成对该订单的取消
            orderService.cancel(orderDTO);
        } catch (Exception e) {
            log.error("[卖家端取消订单]发生异常{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

    //3接口：查看订单详情
    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String, Object> map) {
        OrderDTO orderDTO;
        try {
            orderDTO=orderService.findOne(orderId);
        } catch (Exception e) {
            log.error("[卖家端查询订单详情]发生异常{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }

        map.put("orderDTO",orderDTO);
        return new ModelAndView("order/detail",map);
    }

    //2接口：取消订单
    @RequestMapping("/finish")
    public ModelAndView  finished(@RequestParam("orderId") String orderId,
                                Map<String,Object> map) {
        try {
            //1根据订单号获取取消的订单
            OrderDTO orderDTO=orderService.findOne(orderId);
            //2调用orderService的cancel方法完成对该订单的取消
            orderService.finish(orderDTO);
        } catch (Exception e) {
            log.error("[卖家端完结订单]发生异常{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }



}
